__file__ = 'A16.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-14'

"""
    问题：纯文本文件 numbers.txt为城市信息, 里面的内容（包括花括号）如下所示：
        [
            [1, 82, 65535],
            [20, 90, 13],
            [26, 809, 1024]
        ]
        请将上述内容写到 numbers.xls 文件中，如下图所示：
"""

from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter

def getdata():
    # 读取文档数据
    f = open('d://numbers.txt').read()
    # 转换为dict类型
    d = list(eval(f))
    savefile(d)

def savefile(data):
    # 新建excel
    wb = Workbook()
    ew = ExcelWriter(workbook=wb)
    filepath = 'd://numbers.xlsx'
    # 第一个sheet
    ws = wb.worksheets[0]
    row = 1
    for i in range(len(data)):
        linedata = data[i]
        c = 1
        for vi in range(len(linedata)):
            ws.cell(row=row, column=c, value=str(linedata[vi]))
            c += 1
        row += 1

    # 保存
    ew.save(filepath)
    print('保存成功')

if __name__ == '__main__':
    getdata()